﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IPort;
using YTTest;

namespace AELTAPLC
{
    public class DevPLC_AELTA
    {

        public string DevName { get; set; }

        public IPort.ILogicPort _Port = null;

      //  string IPLC.IPLC.Port { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        /// <summary>
        /// 初始化，连接PLC
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool Init()
        {
            if (this._Port == null) return false;
            if (!this._Port.IsOpen) return false;
            return true;
        }
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public bool Close(object args)
        {
            if (_Port == null) return true;
            _Port.Close(string.Empty);
            return true;
        }
        /// <summary>
        /// gfvfgt
        /// </summary>
        /// <param name="regAddr"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public ushort[] ReadRegister(byte cmdCode, string regAddr, int length)
        {
            try
            {
                //System.Threading.Thread.Sleep(200);
                if (this._Port == null) return null;
                string addr = regAddr.Substring(0, 1);
                string reg = regAddr.Substring(1);
                if (!Enum.IsDefined(typeof(InTaPLC.起始地址), addr)) return null;
                int ireg = 0;
                if (!int.TryParse(reg, out ireg)) return null;
                InTaPLC.起始地址 s = (InTaPLC.起始地址)Enum.Parse(typeof(InTaPLC.起始地址), addr);
                ushort v = (ushort)length;
                byte[] buffer = new byte[2];
                buffer[0] = (byte)(v / 256);
                buffer[1] = (byte)(v % 256);
                byte[] dta = AeltaPLC.发送指令(this._Port, InTaPLC.从站地址.PLC1, (InTaPLC.命令码)cmdCode, s, ireg, buffer);

                if (dta != null && dta.Length % 2 == 0)
                {
                    IOHelper.DebugMsg.Log(this.DevName + "   ReadRegister：" + BitConverter.ToString(dta));
                    ushort[] res = new ushort[dta.Length / 2];
                    for (int i = 0; i < dta.Length; i += 2)
                    {
                        byte[] bg = new byte[2];
                        bg[0] = dta[i + 1];
                        bg[1] = dta[i];

                        res[i / 2] = BitConverter.ToUInt16(bg, 0);
                    }
                    return res;
                }
                else
                {
                    IOHelper.DebugMsg.Log(this.DevName + "   ReadRegister：" + null);
                    return null;
                }
            }
            catch (Exception ex)
            {
                IOHelper.DebugMsg.Log(this.DevName + "   ReadRegister Error：" +ex.Message+ex.StackTrace);
                return null;
            }
               
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="regAddr"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public byte ReadIO(string regAddr)
        {
            try
            {

                //System.Threading.Thread.Sleep(200);
                if (this._Port == null) return 0x00;
                string addr = regAddr.Substring(0, 1);
                string reg = regAddr.Substring(1);
                if (!Enum.IsDefined(typeof(InTaPLC.起始地址), addr)) return 0x00;
                int ireg = 0;
                if (!int.TryParse(reg, out ireg)) return 0x00;
                int index = ireg % 8;
                ireg = ireg / 8;
                ireg *= 8;
                InTaPLC.起始地址 s = (InTaPLC.起始地址)Enum.Parse(typeof(InTaPLC.起始地址), addr);
                byte[] buffer = new byte[2];
                buffer[0] = 0x00;
                buffer[1] = 0x08;
                byte[] dta = AeltaPLC.发送指令(this._Port, InTaPLC.从站地址.PLC1, InTaPLC.命令码.读节点状态, s, ireg, buffer);

                if (dta != null && dta.Length == 1)
                {
                    IOHelper.DebugMsg.Log(this.DevName + "   ReadIO：" + BitConverter.ToString(dta));
                    return dta[0];

                }
                else
                {
                    IOHelper.DebugMsg.Log(this.DevName + "   ReadIO：" + null);
                    return 0xFF;
                }

            }
            catch (Exception ex)
            {
                 IOHelper.DebugMsg.Log(this.DevName + "   ReadIO Error：" +ex.Message+ex.StackTrace);
                 return 0xff;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="regAddr"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public bool SetRegister(byte regType, string regAddr, ushort val)
        {
            try
            {
                if (this._Port == null) return false;
                string addr = regAddr.Substring(0, 1);
                string reg = regAddr.Substring(1);
                if (!Enum.IsDefined(typeof(InTaPLC.起始地址), addr)) return false;
                int ireg = 0;
                if (!int.TryParse(reg, out ireg)) return false;
                InTaPLC.起始地址 s = (InTaPLC.起始地址)Enum.Parse(typeof(InTaPLC.起始地址), addr);
                byte[] buffer = BitConverter.GetBytes(val);
                byte vvm = buffer[0];
                buffer[0] = buffer[1];
                buffer[1] = vvm;
                byte[] dta = AeltaPLC.发送指令(this._Port, InTaPLC.从站地址.PLC1, (InTaPLC.命令码)regType, s, ireg, buffer);

                return dta != null;
            }
            catch (Exception ex)
            {
                IOHelper.DebugMsg.Log(this.DevName + "   SetRegister Error：" +ex.Message+ex.StackTrace);
                return false;
            }
        }


        /// <summary>
        /// 台达PLC读浮点数
        /// </summary>
        /// <param name="regAddr"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public ushort[] ReadRegisterDB(byte cmdCode, string regAddr, int length, byte devID = 0x01)
        {
            try
            {
                //System.Threading.Thread.Sleep(200);
                if (this._Port == null) return null;
                string addr = regAddr.Substring(0, 1);
                string reg = regAddr.Substring(1);
                if (!Enum.IsDefined(typeof(InTaPLC.起始地址), addr)) return null;
                int ireg = 0;
                if (!int.TryParse(reg, out ireg)) return null;
                InTaPLC.起始地址 s = (InTaPLC.起始地址)Enum.Parse(typeof(InTaPLC.起始地址), addr);
                ushort v = (ushort)length;
                byte[] buffer = new byte[2];
                buffer[0] = (byte)(v / 256);
                buffer[1] = (byte)(v % 256);
                byte[] dta = AeltaPLC.发送指令(this._Port, (InTaPLC.从站地址)devID, (InTaPLC.命令码)cmdCode, s, ireg, buffer);

                if (dta != null && dta.Length % 2 == 0)
                {
                    IOHelper.DebugMsg.Log(this.DevName + "   ReadRegister：" + BitConverter.ToString(dta));
                    ushort[] res = new ushort[dta.Length / 2];
                    for (int i = 0; i < dta.Length; i += 2)
                    {
                        byte[] bg = new byte[2];
                        bg[0] = dta[i + 1];
                        bg[1] = dta[i];

                        res[i / 2] = BitConverter.ToUInt16(bg, 0);
                    }
                    return res;
                }
                else
                {
                    IOHelper.DebugMsg.Log(this.DevName + "   ReadRegister：" + null);
                    return null;
                }
            }
            catch (Exception ex)
            {
                IOHelper.DebugMsg.Log(this.DevName + "   ReadRegister Error：" + ex.Message + ex.StackTrace);
                return null;
            }

        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="startRegAddr"></param>
        /// <param name="length"></param>
        /// <param name="vals"></param>
        /// <returns></returns>
        public bool SetRegisters(string regType, string startRegAddr, int length, ushort[] vals)
        {
            // throw new NotImplementedException();
            return false;
        }


        public float[] ReadRegisterF(byte regType, string regAddr, int length)
        {
            throw new NotImplementedException();
        }
    }
}
